local heyi = fk.CreateSkill{
  name = "ld__heyi",
  tags = { Skill.Compulsory },
}

local H = require "packages.ol_hegemony.util"

heyi:addEffect("arraysummon", {
    array_type = "formation",
})

local heyi_spec = {
  can_refresh = function(self, event, target, player, data)
      return player:hasShownSkill(heyi.name, true, true)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      local ret = #room.alive_players > 3 and player:hasSkill(heyi.name) and H.inFormationRelation(p, player) and player:hasShownSkill(heyi.name)
      room:handleAddLoseSkills(p, ret and "ld__feiying" or "-ld__feiying", nil, false, true)
       if ret and player:hasShownSkill(heyi.name, true, true) then
        room:setPlayerMark(p, "@@heyi", 1)
      else
        room:setPlayerMark(p, "@@heyi", 0)
      end
    end
  end,
}

heyi:addEffect(fk.TurnStart, heyi_spec)
heyi:addEffect(fk.GeneralRevealed, heyi_spec)
heyi:addEffect(fk.EventAcquireSkill, heyi_spec)
heyi:addEffect(fk.GeneralHidden, {
  can_refresh = function (self, event, target, player, data)
    return target == player
  end,
  on_refresh = heyi_spec.on_refresh,
})
heyi:addEffect(H.GeneralRemoved, heyi_spec)
heyi:addEffect(H.PlayerRemoved, heyi_spec)
heyi:addEffect(fk.Deathed, heyi_spec)

--失去技能
heyi:addLoseEffect(function (self, player, is_death)
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      if p:hasSkill("ld__feiying") then
      room:handleAddLoseSkills(p, "-ld__feiying", nil, false, true)
      end
      if p:getMark("@@heyi") > 0 then
      room:setPlayerMark(p, "@@heyi", 0)
      end
    end
  end)

heyi:addTest(function (room, me)
    FkTest.runInRoom(function ()
      room:handleAddLoseSkills(me, heyi.name)
    end)
  end)

Fk:loadTranslationTable{
    ["ld__heyi"] = "鹤翼",
    [":ld__heyi"] = "<a href='heg_formation_skill'>阵法技</a>，与你处于同一<a href='heg_formation'>队列</a>的角色拥有〖飞影〗。",
}

return heyi